package cn.newtest.common.dbutils;

import java.io.Serializable;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author wangchui(baiyibing@gmail.com)
 * @version 1.0 TODO
 * 
 */
public class DbRow extends HashMap implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -4390369781728540657L;
	private Map result = new CaseInsensitiveHashMap();

	public DbRow() {
	}

	public boolean containsKey(Object key) {
		return result.containsKey(key.toString().toLowerCase());
	}

	/**
	 * get a object value from the DBRow
	 * 
	 * @param key
	 * @return
	 */
	public Object getObject(String key) {
		return result.get(key);
	}

	/**
	 * get a string value from the DBRow
	 * 
	 * @param key
	 * @return
	 */
	public String getString(String key) {

		Object obj = null;
		obj = this.getObject(key);
		String retStr = null;

		if (obj != null) {
			if (obj instanceof java.sql.Date) {
				java.sql.Date dateTemp = (java.sql.Date) obj;
				Calendar c = Calendar.getInstance();
				c.setTime(dateTemp);
				retStr = c.get(Calendar.YEAR) + "-"
						+ (c.get(Calendar.MONTH) + 1) + "-"
						+ c.get(Calendar.DAY_OF_MONTH);
			} else if (obj instanceof java.sql.Time) {
				java.sql.Date dateTemp = (java.sql.Date) obj;
				Calendar c = Calendar.getInstance();
				c.setTime(dateTemp);
				retStr = c.get(Calendar.YEAR) + "-"
						+ (c.get(Calendar.MONTH) + 1) + "-"
						+ c.get(Calendar.DAY_OF_MONTH);
			} else if (obj instanceof java.sql.Timestamp) {
				java.sql.Timestamp dateTemp = (java.sql.Timestamp) obj;
				Calendar c = Calendar.getInstance();
				c.setTime(dateTemp);
				retStr = c.get(Calendar.YEAR) + "-"
						+ (c.get(Calendar.MONTH) + 1) + "-"
						+ c.get(Calendar.DAY_OF_MONTH);
			} else {
				retStr = obj.toString();
				// System.out.println("如果是一个CLOB呢："+retStr);
			}
		}
		return retStr;
	}

	/**
	 * add a object value to DBRow
	 * 
	 * @param key
	 *            object value of the keys in DBRow
	 * @param value
	 *            object value
	 */
	public Object put(String key, Object data) {
		return result.put(key, data);
	}

	/**
	 * remove all values from DBRow
	 */
	public Object remove(Object key) {
		return result.remove(key);
	}

	/**
	 * A Map that converts all keys to lowercase Strings for case insensitive
	 * lookups. This is needed for the toMap() implementation because databases
	 * don't consistenly handle the casing of column names.
	 */
	private static class CaseInsensitiveHashMap extends HashMap {

		/**
		 * @see java.util.Map#containsKey(java.lang.Object)
		 */
		public boolean containsKey(Object key) {
			return super.containsKey(key.toString().toLowerCase());
		}

		/**
		 * @see java.util.Map#get(java.lang.Object)
		 */
		public Object get(Object key) {
			return super.get(key.toString().toLowerCase());
		}

		/**
		 * @see java.util.Map#put(java.lang.Object, java.lang.Object)
		 */
		public Object put(Object key, Object value) {
			return super.put(key.toString().toLowerCase(), value);
		}

		/**
		 * @see java.util.Map#putAll(java.util.Map)
		 */
		public void putAll(Map m) {
			Iterator iter = m.keySet().iterator();
			while (iter.hasNext()) {
				Object key = iter.next();
				Object value = m.get(key);
				this.put(key, value);
			}
		}

		/**
		 * @see java.util.Map#remove(java.lang.Object)
		 */
		public Object remove(Object key) {
			return super.remove(key.toString().toLowerCase());
		}
	}
}